package com.ktgj.flight.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.ktgj.flight.domain.vo.FlightOverdueTicketVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.flight.mapper.FlightOverdueTicketMapper;
import com.ktgj.flight.domain.FlightOverdueTicket;
import com.ktgj.flight.service.IFlightOverdueTicketService;

/**
 * 逾期未起飞客票Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-23
 */
@Service
public class FlightOverdueTicketServiceImpl implements IFlightOverdueTicketService 
{
    @Autowired
    private FlightOverdueTicketMapper flightOverdueTicketMapper;

    /**
     * 查询逾期未起飞客票
     * 
     * @param id 逾期未起飞客票ID
     * @return 逾期未起飞客票
     */
    @Override
    public FlightOverdueTicket selectFlightOverdueTicketById(Long id)
    {
        return flightOverdueTicketMapper.selectFlightOverdueTicketById(id);
    }

    /**
     * 查询逾期未起飞客票列表
     * 
     * @param flightOverdueTicket 逾期未起飞客票
     * @return 逾期未起飞客票
     */
    @Override
    public List<FlightOverdueTicket> selectFlightOverdueTicketList(FlightOverdueTicket flightOverdueTicket)
    {
        return flightOverdueTicketMapper.selectFlightOverdueTicketList(flightOverdueTicket);
    }

    /**
     * 新增逾期未起飞客票
     * 
     * @param flightOverdueTicket 逾期未起飞客票
     * @return 结果
     */
    @Override
    public int insertFlightOverdueTicket(FlightOverdueTicket flightOverdueTicket)
    {
        return flightOverdueTicketMapper.insertFlightOverdueTicket(flightOverdueTicket);
    }

    /**
     * 修改逾期未起飞客票
     * 
     * @param flightOverdueTicket 逾期未起飞客票
     * @return 结果
     */
    @Override
    public int updateFlightOverdueTicket(FlightOverdueTicket flightOverdueTicket)
    {
        return flightOverdueTicketMapper.updateFlightOverdueTicket(flightOverdueTicket);
    }

    /**
     * 批量删除逾期未起飞客票
     * 
     * @param ids 需要删除的逾期未起飞客票ID
     * @return 结果
     */
    @Override
    public int deleteFlightOverdueTicketByIds(Long[] ids)
    {
        return flightOverdueTicketMapper.deleteFlightOverdueTicketByIds(ids);
    }

    /**
     * 删除逾期未起飞客票信息
     * 
     * @param id 逾期未起飞客票ID
     * @return 结果
     */
    @Override
    public int deleteFlightOverdueTicketById(Long id)
    {
        return flightOverdueTicketMapper.deleteFlightOverdueTicketById(id);
    }

    //逾期未起飞客票监控列表
    @Override
    public List<FlightOverdueTicket> flightOverdueTicketList(FlightOverdueTicketVO overdueTicket) {
        if (overdueTicket.getOutTicketTimeEnd() != null && !overdueTicket.getOutTicketTimeEnd().equals(""))
            overdueTicket.setOutTicketTimeEnd(overdueTicket.getOutTicketTimeEnd() + " 23:59:59");
        if (overdueTicket.getGenerateBBillTimeEnd() != null && !overdueTicket.getGenerateBBillTimeEnd().equals(""))
            overdueTicket.setGenerateBBillTimeEnd(overdueTicket.getGenerateBBillTimeEnd() + " 23:59:59");
        List<FlightOverdueTicket> flightOverdueTickets = flightOverdueTicketMapper.flightOverdueTicketList(overdueTicket);
        if (flightOverdueTickets != null && flightOverdueTickets.size() > 0) {
            for (FlightOverdueTicket flightOverdueTicket : flightOverdueTickets) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                try {
                    Date parse = dateFormat.parse(flightOverdueTicket.getTicketNoInvalidTime());
                    long time = (parse.getTime() - System.currentTimeMillis());
                    if (time > 0) {
                        long diffMinutes = time / (60 * 1000) % 60;
                        long diffHours = time / (60 * 60 * 1000) % 24;

                        String flightTimes = "";
                        if (diffHours == 0) {
                            flightTimes = diffMinutes + "分钟";
                        } else if (diffMinutes == 0) {
                            flightTimes = diffHours + "小时";
                        } else if (diffHours != 0 && diffMinutes != 0) {
                            flightTimes = diffHours + "小时" + diffMinutes + "分钟";
                        }
                        flightOverdueTicket.setTicketNoInvalidTime(flightTimes);
                    } else
                        flightOverdueTicket.setTicketNoInvalidTime("已失效");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        return flightOverdueTickets;
    }
}
